package com.hq.fxh.No3_TuiGuang.Adaoter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.AppCompatRadioButton;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.hq.fxh.CustomerView.CircleImageView;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.Modle.GetEnterpriseRankRes;
import com.hq.fxh.Modle.GetEnterpriseReportRes;
import com.hq.fxh.No3_TuiGuang.Activity.ExportDetailActivity;
import com.hq.fxh.R;
import com.hq.fxh.Utils.GlideUtils;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.LongNumTransUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;
import com.hq.fxh.HttpApi.UserServiceImpl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 小谢
 * @ClassName HeaderEmployeeRankAdapter
 * @Description :
 * @Created 2017/5/17 0017
 */

public class HeaderEmployeeRankAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener, TabLayout.OnTabSelectedListener, RadioGroup.OnCheckedChangeListener {
    //item类型
    public static final int ITEM_TYPE_HEADER = 0;
    public static final int ITEM_TYPE_CONTENT = 1;
    private final int uid;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private List<GetEnterpriseRankRes.DataBean.UsersBean> mDatas;
    private List<String> list_title;
    private int mHeaderCount = 1;//0没有自己的数据 1 有自己的数据
    private int statissType = 1;
    private int staticsDate = 1;
    private int count;
    View myview;

    public void setmHeaderCount(int mHeaderCount) {
        this.mHeaderCount = mHeaderCount;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public static interface MyTabSelcctedListener {
        void onMyTabSelectedListener(TabLayout.Tab view, int staticsDate);
    }

    private MyTabSelcctedListener myTabSelcctedListener = null;

    public void setMyTabSelcctedListener(MyTabSelcctedListener listener) {
        this.myTabSelcctedListener = listener;
    }

    public static interface MyCheckedChangeListener {
        void onMyCheckedChangeListener(int staticsType, int staticsDate);
    }

    private MyCheckedChangeListener myMyCheckedChangeListener = null;

    public void setMyCheckedChangeListener(MyCheckedChangeListener listener) {
        this.myMyCheckedChangeListener = listener;
    }

    public HeaderEmployeeRankAdapter(Context context, List<GetEnterpriseRankRes.DataBean.UsersBean> datas) {
        mContext = context;
        this.mDatas = datas;
        mLayoutInflater = LayoutInflater.from(context);
        uid = SharedPreferenceUtils.getMemberId(mContext);
    }

    //判断当前item类型
    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ITEM_TYPE_HEADER;
        } else {
            return ITEM_TYPE_CONTENT;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_atricle_export:
                toExportDetailActivity(0);
                break;
            case R.id.rl_article_sum:
                toExportDetailActivity(3);
                break;
            case R.id.rl_ad_click:
                toExportDetailActivity(1);
                break;
            case R.id.rl_article_trans:
                toExportDetailActivity(2);
                break;
        }
    }

    private void toExportDetailActivity(int position) {
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
         Intent intent = new Intent(mContext, ExportDetailActivity.class);
        intent.putExtras(bundle);
        mContext.startActivity(intent);

     }

    //OnTabStlectListener
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        if (myTabSelcctedListener != null) {
            //注意这里使用getTag方法获取数据
            myTabSelcctedListener.onMyTabSelectedListener(tab, staticsDate);
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    //OnCheckedChangeListener
    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        if (myMyCheckedChangeListener != null) {
            //注意这里使用getTag方法获取数据
            myMyCheckedChangeListener.onMyCheckedChangeListener(statissType, staticsDate);
        }

    }

    //内容 ViewHolder
    public static class ContentViewHolder extends RecyclerView.ViewHolder {
        TextView rank, name, num;
        CircleImageView head;
        ImageView rank_img;
        LinearLayout ll;
        View split;

        public ContentViewHolder(View view) {
            super(view);
            rank = (TextView) view.findViewById(R.id.employee_rank);
            name = (TextView) view.findViewById(R.id.employee_name);
            num = (TextView) view.findViewById(R.id.employee_num);
            head = (CircleImageView) view.findViewById(R.id.employee_head);
            rank_img = (ImageView) view.findViewById(R.id.employee_rank_img);
            ll = (LinearLayout) view.findViewById(R.id.ll);
            split = view.findViewById(R.id.split);
        }
    }

    //头部 ViewHolder
    public class HeaderViewHolder extends RecyclerView.ViewHolder {
        private TextView save_money;
        private TextView company_name;
        private RadioGroup rg_rank;
        private AppCompatRadioButton rank_sum;
        private AppCompatRadioButton rank_month;
        private LinearLayout rl_atricle_export;
        private LinearLayout rl_article_sum;
        private LinearLayout rl_ad_click;
        private LinearLayout rl_article_trans;
        private TextView article_export_add;
        private TextView article_export_sum;
        private TextView article_export_unit;
        private TextView article_sun_add;
        private TextView article_sum_sum;
        private TextView article_sum_unit;
        private TextView ad_click_sum;
        private TextView ad_click_add;
        private TextView ad_click_rate;
        private TextView article_trans_sum;
        private TextView article_trans_add;
        private TextView article_trans_rate;
        private TabLayout tab_title;
        public RelativeLayout rl_tl;
        public RelativeLayout rl_rg;
        private TextView articleOrder, adOrder;
        private TextView count;

        public HeaderViewHolder(View view) {
            super(view);
            myview = view;
            count = (TextView) view.findViewById(R.id.count);
            rg_rank = (RadioGroup) view.findViewById(R.id.rg_rank);
            rank_month = (AppCompatRadioButton) view.findViewById(R.id.rank_month);
            rank_sum = (AppCompatRadioButton) view.findViewById(R.id.rank_sum);
            save_money = (TextView) view.findViewById(R.id.save_money);
            company_name = (TextView) view.findViewById(R.id.company_name);

            rl_atricle_export = (LinearLayout) view.findViewById(R.id.rl_atricle_export);
            rl_article_sum = (LinearLayout) view.findViewById(R.id.rl_article_sum);
            rl_ad_click = (LinearLayout) view.findViewById(R.id.rl_ad_click);
            rl_article_trans = (LinearLayout) view.findViewById(R.id.rl_article_trans);

            article_export_add = (TextView) view.findViewById(R.id.article_export_add);
            article_export_sum = (TextView) view.findViewById(R.id.article_export_sum);
            article_export_unit = (TextView) view.findViewById(R.id.article_export_unit);
            article_sun_add = (TextView) view.findViewById(R.id.article_sun_add);
            article_sum_sum = (TextView) view.findViewById(R.id.article_sum_sum);
            article_sum_unit = (TextView) view.findViewById(R.id.article_sum_unit);
            ad_click_sum = (TextView) view.findViewById(R.id.ad_click_sum);
            ad_click_add = (TextView) view.findViewById(R.id.ad_click_add);
            ad_click_rate = (TextView) view.findViewById(R.id.ad_click_rate);
            article_trans_sum = (TextView) view.findViewById(R.id.article_trans_sum);
            article_trans_add = (TextView) view.findViewById(R.id.article_trans_add);
            articleOrder = (TextView) view.findViewById(R.id.articleOrder);
            adOrder = (TextView) view.findViewById(R.id.adOrder);
            article_trans_rate = (TextView) view.findViewById(R.id.article_trans_rate);
            tab_title = (TabLayout) view.findViewById(R.id.rank_tab_title);
            rl_tl = (RelativeLayout) view.findViewById(R.id.rl_tl);
            rl_rg = (RelativeLayout) view.findViewById(R.id.rl_rg);
        }
    }

    public View getHeadViewHeight() {
        return myview;
    }

    ;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_HEADER) {
            HeaderViewHolder headViewHowder = new HeaderViewHolder(mLayoutInflater.inflate(R.layout.item_head_employee_rank, parent, false));
            initTabLayout(headViewHowder);
            return headViewHowder;
        } else if (viewType == ITEM_TYPE_CONTENT) {
            return new ContentViewHolder(mLayoutInflater.inflate(R.layout.item_busi_employee, parent, false));
        } else {
            return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //头部
        if (holder instanceof HeaderViewHolder) {
            initBusinessInfo((HeaderViewHolder) holder);
        } else if (holder instanceof ContentViewHolder) {
            GetEnterpriseRankRes.DataBean.UsersBean data = null;//data.getData().getMyself()为null,传递来的mDatas.get(0) == null
            if (mHeaderCount != 0 && position == 1) {
                ((ContentViewHolder) holder).ll.setBackgroundColor(mContext.getResources().getColor(R.color.oriange2));
                ((ContentViewHolder) holder).split.setVisibility(View.VISIBLE);
            } else {
                ((ContentViewHolder) holder).ll.setBackgroundColor(Color.WHITE);
                ((ContentViewHolder) holder).split.setVisibility(View.GONE);
            }
            topHeadDeal((ContentViewHolder) holder);

            data = mDatas.get(position - 1);
            ((ContentViewHolder) holder).rank.setText(data.getRank_num() + "");
            ((ContentViewHolder) holder).name.setText(data.getUser_nick_name());
            String count = "<font color=\"#d81b24\">" + data.getValue() + "</font>" + "次";//16
            ((ContentViewHolder) holder).num.setText(Html.fromHtml(count));
             GlideUtils.loadImageView(mContext,data.getUser_heder_url(),((ContentViewHolder) holder).head);
            switch (data.getRank_num()) {
                case 1:
                    ((ContentViewHolder) holder).rank_img.setImageResource(R.mipmap.qy_one);
                    break;
                case 2:
                    ((ContentViewHolder) holder).rank_img.setImageResource(R.mipmap.qy_two);
                    break;
                case 3:
                    ((ContentViewHolder) holder).rank_img.setImageResource(R.mipmap.qy_there);
                    break;
                default:
                    ((ContentViewHolder) holder).rank_img.setVisibility(View.GONE);
                    ((ContentViewHolder) holder).rank.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

    private void topHeadDeal(ContentViewHolder holder) {
        holder.rank_img.setVisibility(View.VISIBLE);
        holder.rank.setVisibility(View.GONE);
    }

    @Override
    public int getItemCount() {
        if (mDatas == null) {
            return 1;
        } else {
            return mDatas.size() + 1;
        }
    }

    private void initBusinessInfo(final HeaderViewHolder holder) {
        String cmpName = SharedPreferenceUtils.getCompanyName(mContext);
        holder.company_name.setText(cmpName);
        UserServiceImpl.getEnterpriseReport(uid + "", new NetworkSubscriber<GetEnterpriseReportRes>() {
            @Override
            public int onSuccess(GetEnterpriseReportRes data) {
                super.onSuccess(data);
                if (data != null && data.getCode() == 0) {
                    String str = "<font color=\"#f9ccce\">" + "规模：" + "</font>" + count + "<font color=\"#f9ccce\">" + "人" + "</font>";
                    holder.count.setText(Html.fromHtml(str));
                    GetEnterpriseReportRes.DataBean dataBean = data.getData();
                    DecimalFormat df = new DecimalFormat("###0.00");
                    holder.article_export_add.setText("+" + dataBean.getNum_look_today());
                    final Object[] lookNum = LongNumTransUtils.calculateNum(dataBean.getNum_look_tot());
                    holder.article_export_sum.setText(lookNum[0] + "");
                    holder.article_export_unit.setText(lookNum[1] + "次");
                    holder.article_sun_add.setText("+" + dataBean.getNum_article_today());
                    final Object[] articleNum = LongNumTransUtils.calculateNum(dataBean.getNum_article_tot());
                    holder.article_sum_sum.setText(articleNum[0] + "");
                    holder.article_sum_unit.setText(articleNum[1] + "篇");
                    holder.ad_click_add.setText("+" + dataBean.getNum_click_today());
                    final Object[] adClick = LongNumTransUtils.calculateNum(dataBean.getNum_click_tot());
                    holder.ad_click_sum.setText(adClick[0] + "");
                    holder.adOrder.setText(adClick[1] + "次");
                    holder.ad_click_rate.setText(df.format(dataBean.getClick_rate()) + "");
                    holder.article_trans_add.setText("+" + dataBean.getNum_trans_today());
                    final Object[] articleTrans = LongNumTransUtils.calculateNum(dataBean.getNum_trans_tot());
                    holder.article_trans_sum.setText(articleTrans[0] + "");
                    holder.articleOrder.setText(articleTrans[1] + "次");
                    double rate = dataBean.getTrans_rate();
                    holder.article_trans_rate.setText(df.format(rate) + "");
                    holder.save_money.setText(df.format(dataBean.getMoney()) + "");
                } else {
                    LogUtils.startToash(mContext,data.getMsg());
                 }
                return 0;
            }
        });
        holder.rl_atricle_export.setOnClickListener(this);
        holder.rl_article_sum.setOnClickListener(this);
        holder.rl_ad_click.setOnClickListener(this);
        holder.rl_article_trans.setOnClickListener(this);
        registerListener(holder);
    }

    private void initTabLayout(HeaderViewHolder holder) {
        list_title = new ArrayList<>();
        list_title.add("曝光");
        list_title.add("点击");
        list_title.add("转发");
        list_title.add("文章");
        //设置TabLayout的模式
        holder.tab_title.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        holder.tab_title.addTab(holder.tab_title.newTab().setText(list_title.get(0)));
        holder.tab_title.addTab(holder.tab_title.newTab().setText(list_title.get(1)));
        holder.tab_title.addTab(holder.tab_title.newTab().setText(list_title.get(2)));
        holder.tab_title.addTab(holder.tab_title.newTab().setText(list_title.get(3)));
        statissType = holder.tab_title.getSelectedTabPosition() + 1;
        holder.tab_title.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
    }

    private void registerListener(final HeaderViewHolder holder) {
        holder.rg_rank.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rank_month://月榜
                        staticsDate = 1;
                        if (myTabSelcctedListener != null) {
                            myMyCheckedChangeListener.onMyCheckedChangeListener(statissType, staticsDate);
                        }
                        break;
                    case R.id.rank_sum://总榜
                        staticsDate = 2;
                        if (myTabSelcctedListener != null) {
                            myMyCheckedChangeListener.onMyCheckedChangeListener(statissType, staticsDate);
                        }
                        break;
                }
            }
        });
        holder.tab_title.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                statissType = tab.getPosition() + 1;
                if (myTabSelcctedListener != null) {
                    myTabSelcctedListener.onMyTabSelectedListener(tab, staticsDate);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
